//
//  ImageViewController.m
//  旅游咨询
//
//  Created by 徐斌 on 15/4/21.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "ImageViewController.h"

@interface ImageViewController ()

@end

@implementation ImageViewController

- (void)viewDidLoad {
    
    self.view.backgroundColor = [UIColor blackColor];
    self.anim = YES;
    [self.navigationController setNavigationBarHidden:YES];
//    self.navigationController.navigationBar.translucent = YES;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.headerReferenceSize = CGSizeMake(0, 0);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    
    [self.myCollectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor blackColor];
    flowLayout.itemSize = CGSizeMake(WidthZ, [UIScreen mainScreen].bounds.size.height);
    [self.view addSubview:self.myCollectionView];
    
    self.myCollectionView.pagingEnabled = YES;
    
    self.myCollectionView.contentOffset = CGPointMake(self.index * WidthZ, 0);
    
    // 返回按钮
    UIButton *retunButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    retunButton.backgroundColor = [UIColor yellowColor];
    [retunButton setBackgroundImage:[UIImage imageNamed:@"add_new_poi_back_btn.png"] forState:UIControlStateNormal];
    [retunButton addTarget:self action:@selector(retunButtonAction) forControlEvents:UIControlEventTouchUpInside];
    retunButton.frame = CGRectMake(0, 0, 30, 30);
    
    self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(10, 30, 30, 30)];
    [self.view addSubview:self.buttonView];
    [self.buttonView addSubview:retunButton];
    
    // 下面的动画
    self.myScollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, HeighZ - 100 , WidthZ, 100)];
    
    self.myScollView.backgroundColor = [UIColor clearColor];
    self.myScollView.contentOffset = CGPointMake(0, HeighZ);
    self.myScollView.bounces = NO;
    
    // 上面动画
    self.returnView = [[UIView alloc]initWithFrame:CGRectMake(0, -HeighZ * (64 / HeighZ), WidthZ, HeighZ * (64 / HeighZ))];
    
    self.returnView.backgroundColor = [UIColor clearColor];
    
    // 上面动画上字
    
    self.labeaaa = [[UILabel alloc]initWithFrame:CGRectMake(WidthZ / 2 - 15, 0, 60, HeighZ * 0.038)];
    self.labeaaa.font = [UIFont systemFontOfSize:20];
    
    
    self.labeaaa.textColor = [UIColor whiteColor];
    self.labeaaa.text = @"相册";
    self.labeaaa.font = [UIFont boldSystemFontOfSize:20.0f];
    
    
    [self.view addSubview:self.myScollView];
    [self.view addSubview:self.returnView];
    
    // 下面动画上的字
    self.txtlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, WidthZ * 0.94667, 200)];
    self.txtlabel.backgroundColor = [UIColor clearColor];
    self.txtlabel.numberOfLines = 0;
    self.txtlabel.font = [UIFont systemFontOfSize:17];
    self.txtlabel.textColor = [UIColor whiteColor];
    self.txtlabel.font = [UIFont boldSystemFontOfSize:17.0f];
    // Do any additional setup after loading the view.
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = [self.modelArray objectAtIndex:indexPath.row];
    
    cell.backgroundColor = [UIColor blackColor];
    
   
    
    return cell;
    
    
}

- (void)retunButtonAction
{
    //
    //    UIViewController *VC = vc;
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.modelArray.count;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.anim == YES) {
        
        
        [UIView beginAnimations:@"Animation" context:@"动画"];
        [UIView setAnimationDuration:0.5];
        
        self.returnView.frame = CGRectMake(0, 0, self.view.frame.size.width, 64);
        
        self.labeaaa.frame = CGRectMake(self.view.frame.size.width / 2 - 15, self.returnView.frame.size.height - 30, 60, 30);
        [self.returnView addSubview:self.labeaaa];
        self.txtlabel.text = [[self.modelArray objectAtIndex:indexPath.row ]text];
        
        CGRect frame = self.myScollView.frame;
        
        frame.origin.y =  self.view.frame.size.height - 100;
        
        self.myScollView.frame = frame;
        [self.myScollView addSubview:self.txtlabel];
        [UIView commitAnimations];

        [self.view bringSubviewToFront:self.buttonView];
        self.anim = NO;
        return;
        
    }
    if (self.anim == NO) {
        [UIView beginAnimations:@"Animation" context:@"动画"];
        [UIView setAnimationDuration:0.5];
        
        self.returnView.frame = CGRectMake(0, -64, self.view.frame.size.width, 64);
        
        CGRect frame = self.myScollView.frame;
        
        frame.origin.y =  self.view.frame.size.height;
        
        self.myScollView.frame = frame;
        
        [UIView commitAnimations];
        self.anim = YES;
        return;
    }
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.txtlabel.text = [[self.modelArray objectAtIndex:(scrollView.contentOffset.x / self.view.frame.size.width)] text];
    CGRect fram = self.txtlabel.frame;
    fram.size.height = [[[DataHandle sharDataBaseHandle] class] getHeighContent:self.txtlabel.text];
    //        fram = self.myScollView.frame;
    self.txtlabel.frame = fram;
    self.myScollView.contentSize = CGSizeMake(self.view.frame.size.width, [[[DataHandle sharDataBaseHandle] class] getHeighContent:self.txtlabel.text] + 10);
    [self.myScollView addSubview:self.txtlabel];

}


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    if ([[[self.modelArray objectAtIndex:indexPath.row] photo_info] class] != [NSNull class]) {
//        
//        NSString *w = [[[self.modelArray objectAtIndex:indexPath.row] photo_info] objectForKey:@"w"];
//        NSString *h = [[[self.modelArray objectAtIndex:indexPath.row] photo_info] objectForKey:@"h"];
//        
//        CGSize sizea = CGSizeMake(self.view.frame.size.width - 10, [h floatValue] * ((self.view.frame.size.width - 20) / [w floatValue]));
//        
//        return sizea;
//    }
//    
//    else
//    {
//        CGSize sizea = CGSizeMake(0, 0);
//        return sizea;
//    }
//    
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
